package com.gientech.controller;

import com.gientech.entity.CommonResult;
import com.gientech.entity.LoanInfo;
import com.gientech.service.LoanInfoService;
import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.common.protocol.heartbeat.MessageModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/risk")
public class LoanController {

    private static final Logger logger = LoggerFactory.getLogger(LoanController.class);
    @Autowired
    private LoanInfoService loanInfoService;

    @RequestMapping("/handlerLoanInfo")
    public void handlerLoanInfo() throws Exception {
        DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("consumer");

        consumer.setNamesrvAddr("127.0.0.1:9876");
        consumer.subscribe("LoanTopic", "*");
        consumer.setMessageModel(MessageModel.BROADCASTING);
        consumer.registerMessageListener(new MessageListenerConcurrently() {
            @Override
            public ConsumeConcurrentlyStatus consumeMessage(List<MessageExt> msgs, ConsumeConcurrentlyContext context) {
                System.out.printf("%s Receive Messages: %s %n", Thread.currentThread().getName(), msgs);
                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        consumer.start();
    }

    /**
     * 审批贷款
     *
     * @return
     */
    @RequestMapping("/checkApply")
    public CommonResult checkApply(@RequestBody LoanInfo loanInfo){
        logger.info("审批贷款>>>>>>>>>>>{}");
        loanInfoService.checkApply(loanInfo);
        return CommonResult.success("");
    }
}
